package chp10.home;

import java.util.Scanner;

public class StartGuessPlus {
    private static int userScore = 0;
    private static int opponentScore = 0;

    public static void main(String[] args) {
        System.out.println("--------------------欢迎进入游戏世界--------------------");
        System.out.println();
        System.out.println("\t\t\t\t******************");
        System.out.println("\t\t\t\t\t**\t猜拳\t**");
        System.out.println("\t\t\t\t******************\n");
        Scanner input = new Scanner(System.in);
        String choiceName = selectCharacter(input);
        String name = inputName(input);
        System.out.println(name + " VS " + choiceName);

        if (confirmStart(input)) {
            playGame(input, choiceName, name);
        }

        printFinalScore(name, choiceName);
        input.close();
    }

    private static String selectCharacter(Scanner input) {
        String choiceName = "";
        while (true) {
            System.out.print("请选择对方角色(1: 刘备 2: 孙权 3: 曹操): ");
            try {
                int choice = input.nextInt();
                if (choice == 1) {
                    return "刘备";
                } else if (choice == 2) {
                    return "孙权";
                } else if (choice == 3) {
                    return "曹操";
                } else {
                    System.out.println("请输入1, 2, 3");
                }
            } catch (Exception e) {
                System.out.println("请输入有效的数字！");
                input.nextLine(); // 清除缓冲区
            }
        }
    }

    private static String inputName(Scanner input) {
        System.out.print("请输入你的姓名: ");
        return input.next();
    }

    private static boolean confirmStart(Scanner input) {
        System.out.print("要开始游戏吗? (Y/N): ");
        String answer = input.next();
        return answer.equalsIgnoreCase("Y");
    }

    private static void playGame(Scanner input, String choiceName, String name) {
        while (true) {
            int guess = getValidGuess(input);
            int random = (int) (Math.random() * 3) + 1;
            String[] options = {"剪刀", "石头", "布"};
            System.out.println(choiceName + "选择了: " + options[random - 1]);

            if (guess == random) {
                System.out.println("结果: 平局!");
            } else if ((guess == 1 && random == 2) || (guess == 2 && random == 3) || (guess == 3 && random == 1)) {
                System.out.println("结果: 再接再厉, 你输了!");
                opponentScore++;
            } else {
                System.out.println("结果: 恭喜, 你赢了!");
                userScore++;
            }

            printCurrentScore(name, choiceName);
            if (!confirmContinue(input)) {
                break;
            }
        }
    }

    private static int getValidGuess(Scanner input) {
        while (true) {
            System.out.print("请输入你的猜测: 1. 剪刀 2. 石头 3. 布 (请输入相应的数字): ");
            try {
                int guess = input.nextInt();
                if (guess >= 1 && guess <= 3) {
                    return guess;
                } else {
                    System.out.println("请输入1到3之间的数字！");
                }
            } catch (Exception e) {
                System.out.println("请输入有效的数字！");
                input.nextLine(); // 清除缓冲区
            }
        }
    }

    private static boolean confirmContinue(Scanner input) {
        System.out.print("是否继续本轮游戏? (Y/N): ");
        String answer = input.next();
        return answer.equalsIgnoreCase("Y");
    }

    private static void printFinalScore(String name, String choiceName) {
        System.out.println("最终得分:");
        System.out.println(name + " 得分: " + userScore);
        System.out.println(choiceName + " 得分: " + opponentScore);
    }

    private static void printCurrentScore(String name, String choiceName) {
        System.out.println(name + " 得分: " + userScore + " | " + choiceName + " 得分: " + opponentScore);
    }
}